using UnityEngine;
using System.Collections.Generic;

public class Interactive : MonoBehaviour
{
	[Header("Interactive")]
	public FingerInput input;

	public PosType posType;
	public enum PosType
	{
		Center,
		TopLeft,
	}

	//[HideInInspector]
	public Tile currentTile = null;
	public List<Tile> selectArea = new List<Tile>();
	public GameObject fxActive;
	public GameObject fxAppear;
	public bool hidden = false;
	
	public ActionType actionType;
	public enum ActionType
	{
		None,
		Attack_Zombie,
		Attack_Player,
		PickUp,
		Action,
	}

	private InteractiveInfo interInfo;
	public InteractiveInfo GetInfo()
	{
		return interInfo;
	}

	public T GetInfo<T>() where T : InteractiveInfo
	{
		return interInfo as T;
	}

	protected bool activated;
	protected string fxActiveName;
	protected string fxAppearName;
	protected List<Tile> pathForInteraction;
	protected Transform trans;


	#region MONO Methods
	protected virtual void Awake()
	{
		trans = transform;

		if (input != null)
		{
			input.MouseTap += FingerClick;
		}
	}

	protected virtual void Start()
	{
		//RecalculateAreas();
	}

	public void Init(InteractiveInfo _info)
	{
		if (PoolManager.Exists())
		{
			if (fxActive != null)
			{
				PoolManager.Instance.CreatePool(fxActive, 1);
				fxActiveName = fxActive.name;
				fxActive = null;
			}
			if (fxAppear != null)
			{
				PoolManager.Instance.CreatePool(fxAppear, 1);
				fxAppearName = fxAppear.name;
				fxAppear = null;
			}
		}

		interInfo = _info;
		if (interInfo != null)
		{
			gameObject.SetActive(!interInfo.hidden);
		}

		RecalculateAreas();
		FloorManager.Instance.RegisterInteractive(this);
	}

	protected virtual void OnDestroy()
	{
		if (FloorManager.Exists())
		{
			FloorManager.Instance.UnregisterInteractive(this);
		}

		if (input != null)
		{
			input.MouseTap -= FingerClick;
		}
	}
	#endregion

	// Returns true if the interactive has been activated
	public virtual bool ActivateInteractive()
	{
		if (!gameObject.activeSelf)
		{
			interInfo.hidden = false;
			gameObject.SetActive(true);
			fxAppear = PoolManager.Instance.GetFromPool(fxAppearName, trans.position, trans.rotation);
			fxAppear.GetComponent<SpriteRenderer>().color = interInfo.mainColor;
			return true;
		}

		return false;
	}

	public virtual void RecalculateAreas()
	{
		if (this != null) // Recently been destroyed
		{
			currentTile = FloorManager.Instance.GetTile(trans.position);
		}
	}

	// Must be overriden to be useful
	protected virtual void FingerClick()
	{
		Debug.Log("Anything");
		// Do Nothing
	}

	#region Select Object
	public virtual bool SetAsSelectable(List<Tile> _path)
	{
		if (!activated)
		{
			activated = true;
			pathForInteraction = _path;

			fxActive = PoolManager.Instance.GetFromPool(fxActiveName);
			fxActive.transform.parent = trans;
			fxActive.transform.localPosition = Vector3.zero;
			fxActive.transform.localRotation = Quaternion.identity;
			
			fxActive.GetComponent<ReturnClick>().CreateEvent(gameObject, "InteractiveClicked");

			return true;
		}

		return false;
	}
	
	// Must be overriden to be useful
	public virtual void InteractiveClicked()
	{
		// Do nothing
	}
	
	public void DeactivateAsSelect()
	{
		activated = false;
		if (fxActive != null)
		{
			PoolManager.Instance.ReturnToPool(fxActive);
		}
	}
	#endregion

	#region Get Methods
	// Returns only scenery
	protected Scenery GetScenery(Vector3 _position)
	{
		Collider2D col = Physics2D.OverlapPoint(_position, (1 << LayerMask.NameToLayer("Scenery")));
		return col ? col.GetComponent<Scenery>() : null;
	}

	// Will not return a tile
	protected Interactive GetInteractive(Vector3 _position)
	{
		Collider2D[] colliders = Physics2D.OverlapPointAll(_position);
		Interactive inter;
		foreach (Collider2D col in colliders)
		{
			inter = col.GetComponent<Interactive>();
			if (!(inter is Tile))
			{
				return inter;
			}
		}

		return null;
	}
	#endregion

	protected virtual void OnDrawGizmosSelected()
	{
		if (UnityEditor.EditorApplication.isPlaying)
		{
			foreach (Tile tile in selectArea)
			{
				Gizmos.DrawIcon(tile.transform.position, "Scenery/gizmoAreaSelect");
			}
		}
	}
}
